package com.ifdom.thread.threadpool;

import java.util.concurrent.*;

/**
 * AbortPolicy： 拒绝策略为 AbortPolicy 策略，直接抛出异常
 *
 * @Author ifredomvip@gmail.com
 * @Date 2022/10/4 10:13
 * @Version 1.0.0
 * @Description
 **/
public class ThreadPool4CreateMode {

    public static void main(String[] args) {

        // 第一种线程池:固定个数的线程池,可以为每个CPU核绑定一定数量的线程数
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(4);
        // 缓存线程池，无上限
        ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
        // 单一线程池,永远会维护存在一条线程
        ExecutorService singleThreadPool = Executors.newSingleThreadExecutor();
        // 固定个数的线程池，可以执行延时任务，也可以执行带有返回值的任务。
        ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);


        Runnable task = () -> {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        };

        for (int i = 0; i < 10; i++) {
            fixedThreadPool.execute(task);
        }
    }
}
